/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: DemCfg.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: Dem
*
*******************************************************************************/

/** HEADER FILES **/
#include "DemCfg.h"


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/


/** LOCAL DATA DECLARATION **/
static const DemDataElementClassType DemDataElementClass_Voltage;
static const DemDataElementClassType DemDataElementClass_Odometer;
static const DemDataElementClassType DemDataElementClass_SnapshotTestValue;
static const DemDataElementClassType DemDataElementClass_FaultCounter[3u];
static const DemDataElementClassType DemDataElementClass_ExtendedTestValue;
static const DemDidClassType DemDidClassConfig_SnapshotRecord01[DTCSnapshotIdentification_SnapshotRecord01_Max];
static const DemDidClassType DemDidClassConfig_SnapshotRecord02[DTCSnapshotIdentification_SnapshotRecord02_Max];
static const DemFreezeFrameClassType DemFreezeFrameClassConfig[DemFreezeFrameRecordNumber_Max];
static const DemFreezeFrameRecordClassType DemFreezeFrameRecordClassConfig[DemFreezeFrameRecordNumber_Max];
static const DemFreezeFrameRecNumClassType DemFreezeFrameRecNumClassConfig;
static const DemExtendedDataRecordClassType Dem_ExtendedDataRecordClassConfig_ExtendedDataRecord01;
static const DemExtendedDataRecordClassType Dem_ExtendedDataRecordClassConfig_ExtendedDataRecord02;
static const DemExtendedDataClassType Dem_ExtendedDataClassConfig[DTCExtendedDataRecordNumber_Max];
static const Dem_DTCAttributesType Dem_DTCAttributesConfig[1u];
static const Dem_DTCType Dem_DtcConfig[DemConf_DemEventId_Max];
static const Dem_OperationCycleType Dem_OperationCycleConfig[1u];
static const Dem_DebounceTimeBasedClassType Dem_DebounceTimeBasedClassConfig[DemConf_DemEventId_Max];
static const Dem_DebounceCounterBasedClassType Dem_DebounceCounterBasedClassConfig[DemConf_DemEventId_Max];


/** LOCAL DATA **/
const DemNvRamBlockIdType DemNvRamBlockIdMapTable[DemConf_NvmBlockId_Max] = 
{
    {NvmConf_Block_DtcDataBlock0},
    {NvmConf_Block_DtcDataBlock1},
    {NvmConf_Block_DtcDataBlock2},
    {NvmConf_Block_DtcDataBlock3},
    {NvmConf_Block_DtcDataBlock4},
    {NvmConf_Block_DtcDataBlock5},
    {NvmConf_Block_DtcDataBlock6},
    {NvmConf_Block_DtcDataBlock7},
    {NvmConf_Block_DtcDataBlock8},
    {NvmConf_Block_DtcDataBlock9}
};

static const DemDataElementClassType DemDataElementClass_Voltage = 
{
    /* Size,   ReadFnc */
    {1u,      Dem_GetElementDataVoltage}, /* Voltage */
};

static const DemDataElementClassType DemDataElementClass_Odometer = 
{
    /* Size,   ReadFnc */
    {3u,      Dem_GetElementDataOdometer}, /* Voltage */
};

static const DemDataElementClassType DemDataElementClass_SnapshotTestValue = 
{
    /* Size,   ReadFnc */
    {2u,      Dem_GetElementDataSnapshotTestValue}, /* SnapshotTestValue */
};

static const DemDataElementClassType DemDataElementClass_FaultCounter[3u] = 
{
    /* Size,   ReadFnc */
    {1u,      Dem_GetElementDataFaultDetectionCounter}, /* SnapshotTestValue */
    {1u,      Dem_GetElementDataAgingCounter}, /* SnapshotTestValue */
    {1u,      Dem_GetElementDataAgedCounter}, /* SnapshotTestValue */
};

static const DemDataElementClassType DemDataElementClass_ExtendedTestValue = 
{
    /* Size,  ReadFnc */
    {2u,      Dem_GetElementDataExtendedTestValue}, /* ExtendedTestValue */
};

static const DemDidClassType DemDidClassConfig_SnapshotRecord01[DTCSnapshotIdentification_SnapshotRecord01_Max] = 
{
    /* identifier   DemDidDataElementClassRef                                             DemDidDataElementClassRefIndexMax */
    {0x1200,      (DemDataElementClassType*)&DemDataElementClass_Voltage,               1u},
    {0x1300,      (DemDataElementClassType*)&DemDataElementClass_Odometer,              1u},
    {0x1400,      (DemDataElementClassType*)&DemDataElementClass_SnapshotTestValue,     1u}
};

static const DemDidClassType DemDidClassConfig_SnapshotRecord02[DTCSnapshotIdentification_SnapshotRecord02_Max] = 
{
    /* identifier   DemDidDataElementClassRef                                             DemDidDataElementClassRefIndexMax */
    {0x1400,      (DemDataElementClassType*)&DemDataElementClass_SnapshotTestValue,    1u}
};

static const DemFreezeFrameClassType DemFreezeFrameClassConfig[DemFreezeFrameRecordNumber_Max] = 
{
    /* didClassRef                                                DemDidClassRefIndexMax*/
    {(DemDidClassType*)DemDidClassConfig_SnapshotRecord01,     DTCSnapshotIdentification_SnapshotRecord01_Max},
    {(DemDidClassType*)DemDidClassConfig_SnapshotRecord02,     DTCSnapshotIdentification_SnapshotRecord02_Max},
};

static const DemFreezeFrameRecordClassType DemFreezeFrameRecordClassConfig[DemFreezeFrameRecordNumber_Max] = 
{
    /* recordNumber,   recordTrigger,             recordUpdate */
    {0x01,             DEM_TRIGGER_ON_CONFIRMED,  DEM_UPDATE_RECORD_NO},
    {0x02,             DEM_TRIGGER_ON_CONFIRMED,  DEM_UPDATE_RECORD_NO}
};

static const DemFreezeFrameRecNumClassType DemFreezeFrameRecNumClassConfig = 
{
    /* freezeFrameRecordClassRef */
    (DemFreezeFrameRecordClassType*)&DemFreezeFrameRecordClassConfig[0u],
    (DemFreezeFrameRecordClassType*)&DemFreezeFrameRecordClassConfig[1u],
};

static const DemExtendedDataRecordClassType Dem_ExtendedDataRecordClassConfig_ExtendedDataRecord01 = 
{
    /* dataElementClassRef,                                                DemDataElementClassRefIndexMax     recordNumber, recordTrigger,              recordUpdate */
    (DemDataElementClassType*)DemDataElementClass_FaultCounter,         3u,                                0x01,         DEM_TRIGGER_ON_CONFIRMED,   DEM_UPDATE_RECORD_NO
};

static const DemExtendedDataRecordClassType Dem_ExtendedDataRecordClassConfig_ExtendedDataRecord02 = 
{
    /* dataElementClassRef,                                                DemDataElementClassRefIndexMax     recordNumber, recordTrigger,              recordUpdate */
    (DemDataElementClassType*)&DemDataElementClass_ExtendedTestValue,   1u,                                0x02,         DEM_TRIGGER_ON_CONFIRMED,   DEM_UPDATE_RECORD_NO
};

static const DemExtendedDataClassType Dem_ExtendedDataClassConfig[DTCExtendedDataRecordNumber_Max] = 
{
    /* extendedDataRecordClassRef */
    {(DemExtendedDataRecordClassType*)&Dem_ExtendedDataRecordClassConfig_ExtendedDataRecord01},
    {(DemExtendedDataRecordClassType*)&Dem_ExtendedDataRecordClassConfig_ExtendedDataRecord02}
};

static const Dem_DTCAttributesType Dem_DTCAttributesConfig[1u] = 
{
    /*DemMaxNumberExtendedDataRecords,  extendedDataClassRef,                                        maxNumberFreezeFrameRecords,       freezeFrameRecNumClassRef,                                                freezeFrameClassRef,                                         agingCycleCounterThreshold, agingCycleCounterThresholdForTFSLC, dtcPriority,   dtcSignificance */
    { DTCExtendedDataRecordNumber_Max,  (DemExtendedDataClassType*)&Dem_ExtendedDataClassConfig,   DemFreezeFrameRecordNumber_Max,   (DemFreezeFrameRecNumClassType*)&DemFreezeFrameRecNumClassConfig,      (DemFreezeFrameClassType*)DemFreezeFrameClassConfig,        1u,                         1u,                                 1u,            DEM_EVENT_SIGNIFICANCE_FAULT}
};

static const Dem_DTCType Dem_DtcConfig[DemConf_DemEventId_Max] = 
{
    /* dtcValue,  dtcAttributesRef,                                      dtcSeverity,              nvStorageStrategy */
    {0xA60016u,  (Dem_DTCAttributesType*)&Dem_DTCAttributesConfig[0], DEM_SEVERITY_NO_SEVERITY, DEM_NV_STORAGE_IMMEDIATE_AT_FIRST_OCCURRENCE}, /* DTC_0x000001 */
    {0xA60017u,  (Dem_DTCAttributesType*)&Dem_DTCAttributesConfig[0], DEM_SEVERITY_NO_SEVERITY, DEM_NV_STORAGE_IMMEDIATE_AT_FIRST_OCCURRENCE}, /* DTC_0x000002 */
    {0xA63303u,  (Dem_DTCAttributesType*)&Dem_DTCAttributesConfig[0], DEM_SEVERITY_NO_SEVERITY, DEM_NV_STORAGE_IMMEDIATE_AT_FIRST_OCCURRENCE}, /* DTC_0x000003 */
};

static const Dem_OperationCycleType Dem_OperationCycleConfig[1u] = 
{
    {0x01u}
};

static const Dem_DebounceTimeBasedClassType Dem_DebounceTimeBasedClassConfig[DemConf_DemEventId_Max] = 
{
    /* failedThreshold, passedThreshold,  behavior,             fdcThresholdStorageValue */
    {127,               -128,             DEM_DEBOUNCE_FREEZE,  0x00u},
    {127,               -128,             DEM_DEBOUNCE_FREEZE,  0x00u},
    {127,               -128,             DEM_DEBOUNCE_FREEZE,  0x00u}
};
    
static const Dem_DebounceCounterBasedClassType Dem_DebounceCounterBasedClassConfig[DemConf_DemEventId_Max] = 
{
    /* incrementStepSize, decrementStepSize,  failedThreshold, passedThreshold, jumpUp, jumpDown, jumpDownValue, jumpUpValue, behavior,             storage, fdcThresholdStorageValue*/
    {20u,                 50u,                127,             -128,            SET,    SET,      1u,            1u,          DEM_DEBOUNCE_FREEZE,  SET,      0u  },
    {20u,                 50u,                127,             -128,            SET,    SET,      1u,            1u,          DEM_DEBOUNCE_FREEZE,  SET,      0u  },
    {20u,                 50u,                127,             -128,            SET,    SET,      1u,            1u,          DEM_DEBOUNCE_FREEZE,  SET,      0u  }
};

const Dem_EventParameterType Dem_EventParameterConfig[DemConf_DemEventId_Max] = 
{
    /* DemEventId,                    DemEventKind,       DemDTCRef,                                                       DemEventConfirmationThreshold,   DemOperationCycleRef,                                     DemDebounceAlgorithmClass */
    {DemConf_DemEventId_DTC_0x000001, DEM_EVENT_KIND_SWC, (Dem_DTCType*)&Dem_DtcConfig[DemConf_DemEventId_DTC_0x000001], 127u,                            (Dem_OperationCycleType*)&Dem_OperationCycleConfig[0u], {{NULL_PTR}, {(Dem_DebounceCounterBasedClassType*)&Dem_DebounceCounterBasedClassConfig[0u]}}}, /* DTC_0x000001 */
    {DemConf_DemEventId_DTC_0x000002, DEM_EVENT_KIND_SWC, (Dem_DTCType*)&Dem_DtcConfig[DemConf_DemEventId_DTC_0x000002], 127u,                            (Dem_OperationCycleType*)&Dem_OperationCycleConfig[0u], {{NULL_PTR}, {(Dem_DebounceCounterBasedClassType*)&Dem_DebounceCounterBasedClassConfig[1u]}}}, /* DTC_0x000002 */
    {DemConf_DemEventId_DTC_0x000003, DEM_EVENT_KIND_SWC, (Dem_DTCType*)&Dem_DtcConfig[DemConf_DemEventId_DTC_0x000003], 127u,                            (Dem_OperationCycleType*)&Dem_OperationCycleConfig[0u], {{NULL_PTR}, {(Dem_DebounceCounterBasedClassType*)&Dem_DebounceCounterBasedClassConfig[2u]}}}  /* DTC_0x000003 */
};

const Dem_EventMemorySetType Dem_EventMemorySetConfig[1u] = 
{
    {
        {
            0x09u /* DemDtcStatusAvailabilityMask */
        }
    }
};


const Dem_ConfigType Dem_Config = 0x00u;

/** LOCAL FUNCTION **/

/*******************************************************************************
* -Name            Dem_GetElementDataVoltage
* -Brief           Gets element data for the voltage
* -Details         None
* -Param[in]       DtcNvmDataPtr: Dtc Nvm Data pointer
* -Param[in]       BufSize: This ExtendedDataRecord buffer size
* -Param[out]      DestBuffer: This parameter contains a byte pointer that points to the buffer
* -Return          E_OK: Size and buffer successfully returned
*                  E_NOT_OK: selection function is not called
*******************************************************************************/
Std_ReturnType Dem_GetElementDataVoltage(
    Dem_DtcNvmDataType* DtcNvmDataPtr,
    uint8 BufSize,
    uint8* DestBuffer
)
{
    Std_ReturnType retVal = E_OK;

    //DestBuffer[0] = (uint8)(DtcNvmDataPtr->AgingCounter >> 8);
    DestBuffer[0] = 0xF1;
    
    return(retVal);
}

/*******************************************************************************
* -Name            Dem_GetElementDataOdometer
* -Brief           Gets element data for the Odometer
* -Details         None
* -Param[in]       DtcNvmDataPtr: Dtc Nvm Data pointer
* -Param[in]       BufSize: This ExtendedDataRecord buffer size
* -Param[out]      DestBuffer: This parameter contains a byte pointer that points to the buffer
* -Return          E_OK: Size and buffer successfully returned
*                  E_NOT_OK: selection function is not called
*******************************************************************************/
Std_ReturnType Dem_GetElementDataOdometer(
    Dem_DtcNvmDataType* DtcNvmDataPtr,
    uint8 BufSize,
    uint8* DestBuffer
)
{
    Std_ReturnType retVal = E_OK;

    //DestBuffer[0] = (uint8)(DtcNvmDataPtr->AgingCounter >> 8);
    //DestBuffer[1] = (uint8)(DtcNvmDataPtr->AgingCounter);
    DestBuffer[0] = 0xF2;
    DestBuffer[1] = 0xF3;
    DestBuffer[2] = 0xF4;

    return(retVal);
}

/*******************************************************************************
* -Name            Dem_GetElementDataSnapshotTestValue
* -Brief           Gets element data for the Snapshot Test Value
* -Details         None
* -Param[in]       DtcNvmDataPtr: Dtc Nvm Data pointer
* -Param[in]       BufSize: This ExtendedDataRecord buffer size
* -Param[out]      DestBuffer: This parameter contains a byte pointer that points to the buffer
* -Return          E_OK: Size and buffer successfully returned
*                  E_NOT_OK: selection function is not called
*******************************************************************************/
Std_ReturnType Dem_GetElementDataSnapshotTestValue(
    Dem_DtcNvmDataType* DtcNvmDataPtr,
    uint8 BufSize,
    uint8* DestBuffer
)
{
    Std_ReturnType retVal = E_OK;

    //DestBuffer[0] = (uint8)(DtcNvmDataPtr->AgingCounter >> 8);
    //DestBuffer[1] = (uint8)(DtcNvmDataPtr->AgingCounter);
    DestBuffer[0] = 0xF5;
    DestBuffer[1] = 0xF6;

    return(retVal);
}

/*******************************************************************************
* -Name            Dem_GetElementDataFaultDetectionCounter
* -Brief           Gets element data for the Number of Fault Detections
* -Details         None
* -Param[in]       DtcNvmDataPtr: Dtc Nvm Data pointer
* -Param[in]       BufSize: This ExtendedDataRecord buffer size
* -Param[out]      DestBuffer: This parameter contains a byte pointer that points to the buffer
* -Return          E_OK: Size and buffer successfully returned
*                  E_NOT_OK: selection function is not called
*******************************************************************************/
Std_ReturnType Dem_GetElementDataFaultDetectionCounter(
    Dem_DtcNvmDataType* DtcNvmDataPtr,
    uint8 BufSize,
    uint8* DestBuffer
)
{
    Std_ReturnType retVal = E_OK;

    //DestBuffer[0] = (uint8)(DtcNvmDataPtr->AgingCounter >> 8);
    //DestBuffer[1] = (uint8)(DtcNvmDataPtr->AgingCounter);
    DestBuffer[0] = 0xF7;

    return(retVal);
}

/*******************************************************************************
* -Name            Dem_GetElementDataAgingCounter
* -Brief           Gets element data for the Aging counter
* -Details         None
* -Param[in]       DtcNvmDataPtr: Dtc Nvm Data pointer
* -Param[in]       BufSize: This ExtendedDataRecord buffer size
* -Param[out]      DestBuffer: This parameter contains a byte pointer that points to the buffer
* -Return          E_OK: Size and buffer successfully returned
*                  E_NOT_OK: selection function is not called
*******************************************************************************/
Std_ReturnType Dem_GetElementDataAgingCounter(
    Dem_DtcNvmDataType* DtcNvmDataPtr,
    uint8 BufSize,
    uint8* DestBuffer
)
{
    Std_ReturnType retVal = E_OK;

    DestBuffer[0] = (uint8)(DtcNvmDataPtr->ExtendedDataAgingCounter);

    return(retVal);
}

/*******************************************************************************
* -Name            Dem_GetElementDataAgedCounter
* -Brief           Gets element data for the Aged counter
* -Details         None
* -Param[in]       DtcNvmDataPtr: Dtc Nvm Data pointer
* -Param[in]       BufSize: This ExtendedDataRecord buffer size
* -Param[out]      DestBuffer: This parameter contains a byte pointer that points to the buffer
* -Return          E_OK: Size and buffer successfully returned
*                  E_NOT_OK: selection function is not called
*******************************************************************************/
Std_ReturnType Dem_GetElementDataAgedCounter(
    Dem_DtcNvmDataType* DtcNvmDataPtr,
    uint8 BufSize,
    uint8* DestBuffer
)
{
    Std_ReturnType retVal = E_OK;

    //DestBuffer[0] = (uint8)(DtcNvmDataPtr->AgingCounter >> 8);
    //DestBuffer[1] = (uint8)(DtcNvmDataPtr->AgingCounter);
    DestBuffer[0] = 0xF9;

    return(retVal);
}

/*******************************************************************************
* -Name            Dem_GetElementDataExtendedTestValue
* -Brief           Gets element data for the Aging counter
* -Details         None
* -Param[in]       DtcNvmDataPtr: Dtc Nvm Data pointer
* -Param[in]       BufSize: This ExtendedDataRecord buffer size
* -Param[out]      DestBuffer: This parameter contains a byte pointer that points to the buffer
* -Return          E_OK: Size and buffer successfully returned
*                  E_NOT_OK: selection function is not called
*******************************************************************************/
Std_ReturnType Dem_GetElementDataExtendedTestValue(
    Dem_DtcNvmDataType* DtcNvmDataPtr,
    uint8 BufSize,
    uint8* DestBuffer
)
{
    Std_ReturnType retVal = E_OK;

    //DestBuffer[0] = (uint8)(DtcNvmDataPtr->Timestamp >> 24);
    //DestBuffer[1] = (uint8)(DtcNvmDataPtr->Timestamp >> 16);
    //DestBuffer[2] = (uint8)(DtcNvmDataPtr->Timestamp >> 8);
    //DestBuffer[3] = (uint8)(DtcNvmDataPtr->Timestamp);
    DestBuffer[0] = 0xFA;
    DestBuffer[1] = 0xFB;

    return(retVal);
}


